<template>
  <div class="wrapper">
    <img src="http://www.dell-lee.com/imgs/vue3/user.png" alt="" class="wrapper__img">
    <div class="wrapper__input">
      <input type="text" class="wrapper__input__content" placeholder="请输入手机号" v-model="username">
    </div>
    <div class="wrapper__input">
      <input type="password" class="wrapper__input__content" placeholder="请输入密码" v-model="password" autocomplete="new-password">
    </div>
    <div class="wrapper__login-button" @click="handleLogin">登陆</div>
    <div class="wrapper__login-link" @click="handleRegisterClick">立即注册</div>
    <Toast v-if="show" :message="toastMessage"/>
  </div>
</template>
<script>
import { reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
// import axios from 'axios'
import { post } from '../../utils/request'// 封装的请求
import Toast, { useToastEffect } from '../../components/Toast'// 封装的弹出层
// axios.defaults.headers.post['Content-Type'] = 'application/json'// 这里有问题

const useLoginEffect = (showToast) => { // showToast作为参数传递进来
  const router = useRouter()// 注册路由实例
  const data = reactive({
    username: '', // 数据到了封装里
    password: ''
  })
  const handleLogin = async () => {
    try {
      const { user, password } = data
      if (user === '' || password === '') {
        showToast('登陆失败')
        return false
      } else { showToast('登陆成功') }
      const result = await post('/api/user/login', {
        username: data.username,
        password: data.password
      })
      if (result?.errno === 0) {
        localStorage.isLogin = true
        router.push({ name: 'Home' })
      } else {
        showToast('登陆失败')
      }
    } catch (e) {
      showToast('请求失败')
    }
  }
  const { username, password } = toRefs(data)
  return { username, password, handleLogin }
}
const useRegisterEffect = () => {
  const router = useRouter()// 注册路由实例
  const handleRegisterClick = () => {
    router.push({ name: 'Register' })
  }
  return { handleRegisterClick }
}
export default {
  name: 'login',
  components: { Toast },
  setup () {
    // 职责就是告诉你执行的流程
    const { show, toastMessage, showToast } = useToastEffect()// 弹窗的逻辑
    const { username, password, handleLogin } = useLoginEffect(showToast)// 登陆的逻辑
    const { handleRegisterClick } = useRegisterEffect()

    return { handleLogin, handleRegisterClick, username, password, show, toastMessage }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables';
.wrapper{
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &__img{
    width: .66rem;
    height: .66rem;
    display: block;
    margin: 0 auto .4rem;
  }
  &__input{
    height: .48rem;
    padding: 0 .16rem;
    margin: 0 .4rem .16rem;
    background-color: #f9f9f9;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: .06rem;
    &__content{
      line-height: .48rem;
      width: 100%;
      border: none;
      outline: none;
      width: 100%;
      background: none;
      color: $content-notice-fontcolor;
      font-size: .16rem;
      &::placeholder{
        color: $content-notice-fontcolor;
        font-size: .16rem;
      }
    }
  }
  &__login-button{
    margin:.32rem .4rem .16rem;
    line-height: .48rem;
    background: $btn-bgColor;
    box-shadow: 0 .04rem .08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: .04rem;
    color: $bgColor;
    font-size: .16rem;
    text-align: center;
  }
  &__login-link{
    text-align: center;
    font-size: .14rem;
    color: $content-notice-fontcolor;
  }
}
</style>
